package com.ruoyi.reservationserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.PageInfo;
import com.ruoyi.reservationserver.dao.CourtDao;
import com.ruoyi.reservationserver.entity.Court;
import com.ruoyi.system.domain.CourtStatistic;
import com.ruoyi.reservationserver.entity.CourtType;
import com.ruoyi.system.domain.Stadium;
import com.ruoyi.reservationserver.pojo.CourtPojo;
import com.ruoyi.reservationserver.service.CourtService;
import com.ruoyi.system.service.CourtStatisticService;
import com.ruoyi.reservationserver.service.CourtTypeService;
import com.ruoyi.reservationserver.service.StadiumService;
import com.ruoyi.reservationserver.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 场地表(Court)表 控制层
 *
 * @author makejava
 * @since 2023-07-04 16:21:47
 */
//@RestController
//@RequestMapping("court")
public class CourtController {
    /**
     * 服务对象
     */
    @Resource
    private CourtService courtService;
    @Resource
    private CourtDao courtDao;

    @Resource
    private CourtTypeService courtTypeService;

    @Resource
    private CourtStatisticService courtStatisticService;

    @Resource
    private StadiumService stadiumService;

    /**
     * 获取分页场地信息
     * @param page
     * @param count
     * @return
     */
    @GetMapping
    public Result index(@RequestParam(value = "page", defaultValue = "1") int page,
                                     @RequestParam(value = "count", defaultValue = "10") int count) {
        PageInfo<Court> pageInfo = courtService.selectCourtWithPage(page, count);
        return Result.success(pageInfo);
    }

    /**
     * 通过场地名称查询场地信息
     * @param courtName
     * @return
     */
    @GetMapping("selectCourtListByCourtName")
    public Result selectCourtListByCourtName(@RequestParam String  courtName){
        List<Court> courtList;
        if (courtName==null){
            return Result.error("不能输入空信息");
        }
        courtList = courtDao.selectCourtListByCourtName(courtName);
        return Result.success(courtList);
    }

    /**
     * 新增数据
     *
     * @param court 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody Court court) {
        QueryWrapper<CourtStatistic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stadium_id",court.getStadiumId());
//        System.out.println("test:"+court.getStadiumId());
        CourtStatistic courtStatistic = courtStatisticService.getOne(queryWrapper);
        if (courtStatistic !=null){
            Integer courtNum = courtStatistic.getCourtNum()+1;
            courtStatistic.setCourtNum(courtNum);
//            System.out.println(courtStatistic);
            courtStatisticService.update(courtStatistic);
        }else {
            return Result.error("500","不存在该球馆");
        }
        return Result.success(this.courtService.insert(court));
    }

    /**
     * 编辑数据
     *
     * @param court 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Court court) {
        System.out.println(court);
        if (court==null){
            return Result.error("返回数据不能为空");
        }

        this.courtService.update(court);
        return Result.success();
    }

    @GetMapping("getCourtList")
    public Result getCourtList(){

        List<CourtPojo> courtPojos=new ArrayList<>();

        List<Court> courts = courtService.list();
        for(Court c :courts){
            CourtPojo courtPojo = new CourtPojo();
            CourtType courtType = courtTypeService.queryById(c.getCourtTypeId());
            courtPojo.setCourtTypeName(courtType.getTypeName());
            courtPojo.setCourtId(c.getCourtId());
            courtPojo.setCapacity(c.getCapacity());
            Stadium stadium = stadiumService.queryById(c.getStadiumId());
            courtPojo.setStadiumName(stadium.getStadiumName());
            courtPojo.setCourtStatus(c.getCourtStatus());
            courtPojo.setCourtName(c.getCourtName());
            courtPojos.add(courtPojo);
        }
        
        return Result.success(courtPojos);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/del")
    public Result deleteById(@RequestParam("courtId") Integer id) {

        try {
            Court court=courtService.queryById(id);
            QueryWrapper<CourtStatistic> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("stadium_id",court.getStadiumId());

            CourtStatistic courtStatistic = courtStatisticService.getOne(queryWrapper);
            if (courtStatistic !=null){
                Integer courtNum = courtStatistic.getCourtNum()-1;
                courtStatistic.setCourtNum(courtNum);
                courtStatisticService.update(courtStatistic);
                this.courtService.deleteById(id);
            }else {
                return Result.error("500","不存在该球馆");
            }


            return Result.success();
        }catch (Exception e) {
            System.out.println(e);
            return Result.error("删除失败");
        }

    }

}

